<template>
  <div>
    <el-container>
      <el-header class="homeHeader">
        <div class="title">员工管理系统</div>
        <!--Dropdown 下拉菜单-->
        <el-dropdown class="userInfo" @command="commandHandler">
          <span class="el-dropdown-link">
            {{user.name}}
            <i><img src="../assets/userFace1.jpg"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
            <el-dropdown-item command="setting">设置</el-dropdown-item>
            <el-dropdown-item command="logout" divided>注销登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>

      <el-container>
        <el-aside width="200px">
          <el-menu router unique-opened>
            <el-submenu :index="index + ''" v-for="(item, index) in routes"
                        :key="index"
                        v-if="!item.hidden">
              <template slot="title">
                <i :class="item.iconCls" class="homeIcon"></i>
                <span>{{item.name}}</span></template>
              <el-menu-item :index="children.path"
                            v-for="(children, indexj) in item.children"
                            :key="indexj">
                {{children.name}}
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-main>
          <!--Breadcrumb 面包屑-->
          <el-breadcrumb v-if="this.$router.currentRoute.path !== '/home'"
                         separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <div v-if="this.$router.currentRoute.path === '/home'" class="homeWelcome">
            欢迎来到 Wzzzy's 员工管理系统！
          </div>
          <router-view class="homeRouterView"/>
        </el-main>

      </el-container>
    </el-container>
  </div>
</template>

<script>
  export default {
    name: "Home",
    data() {
      return {
        // user: JSON.parse(window.sessionStorage.getItem('user'))
        pageToSuccess: false,
        user: {}
      }
    },
    created() {
      this.pageToSuccess = false;
      //加载用户信息
      this.getRequest('/admin/info').then(resp => {
        if(resp){
          this.user = resp;
          //存入sessionStorage中
          window.sessionStorage.setItem('user',JSON.stringify(resp));
          this.pageToSuccess = true;
        }else{
          this.$message({
            type: 'error',
            message: '用户信息加载失败'
          });
        }
      })
    },
    computed: {
      routes() {
        return this.$store.state.routes
      }
    },
    // mounted() {
    //   this.checkUrl()
    // },
    methods: {
      // checkUrl() {
      //   console.log('3')
      //   window.sessionStorage.setItem('url', null)
      // },
      commandHandler(command) {
        if (command == 'logout') {
          //  MessageBox 弹框 - 确认消息
          this.$confirm('此操作将注销登录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //  注销登录
            this.postRequest('/logout')
            //  清空用户信息
            // window.sessionStorage.removeItem('tokenStr')
            // window.sessionStorage.removeItem('user')
            window.sessionStorage.clear()
            //  清空菜单信息，否则下一个用户登录显示的还是上一个用户的菜单
            this.$store.commit('initRoutes', [])
            //  跳转登录页面
            this.$router.replace('/')
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作'
            })
          })
        }
      }
    }
  }
</script>

<style>

  .homeHeader {
    background: dodgerblue;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    box-sizing: border-box;
  }

  .homeHeader .title {
    font-size: 25px;
    color: white;
  }

  .homeHeader .userInfo {
    cursor: pointer;
    color: white;
  }

  .homeIcon {
    color: dodgerblue;
    margin-right: 5px;
  }

  .el-dropdown-link img {
    width: 40px;
    height: 40px;
    border-radius: 20px;
    margin-left: 2px;
  }

  .homeWelcome {
    text-align: center;
    font-size: 30px;
    padding-top: 50px;
  }

  .homeRouterView {
    margin-top: 15px;
  }
</style>